#include "partition.h"
#include <proc/command.h>
#include <proc/reader.h>
#include <proc/exception.h>

Partition::Partition(Connection &connection, const string &table, int method)
{
	this->table = table;
	
	if (method != 4){
		return;
	}
	
	Command command(connection);
	
	command.commandText  = "select a.partitioning_type,c.column_name,b.partition_name,b.high_value ";
	command.commandText += "from user_part_tables a, user_tab_partitions b, user_part_key_columns c ";
	command.commandText += "where a.table_name = b.table_name and a.table_name=c.name ";
	command.commandText += "and a.partitioning_type in ('LIST', 'RANGE') ";
	command.commandText += "and a.table_name=:1 ";
	command.commandText += "and c.object_type='TABLE' order by b.PARTITION_POSITION";
	
	Oracle::Reader *reader = command.prepareReader();
	
	try{
		reader->bindParameter(table.c_str());
		reader->open();
		if (reader->read()){
			PARTITION part;
			partitionType = reader->getData(0);
			column = reader->getData(1);
			part.name = reader->getData(2);
			part.highValue = reader->getData(3);
			partition.push_back(part);
			while (reader->read()){
				PARTITION part;
				part.name = reader->getData(2);
				part.highValue = reader->getData(3);
				partition.push_back(part);
			}
		}
		reader->close();
	}catch(Exception *e){
		printf("%s\n", command.commandText.c_str());
		printf("oracode=%d\n", e->code());
		printf("%s\n", e->what().c_str());
		if (reader != 0){
			reader->close();
			reader = 0;
		}
		delete e;
		return;
	}
}

